package cn.itcast.tanhua.mytanhuaserver.controller;

import cn.itcast.tanhua.mytanhuaserver.service.QuanziSearchService;
import cn.itcast.tanhua.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 郭文亮
 * @since 2021/12/9 10:50
 */
@RestController
@RequestMapping("search")
public class QuanziSearchController {

    @Autowired
    private QuanziSearchService quanziSearchService;

    @GetMapping("synchro")
    public ResponseEntity<Void> synchroEsData() {
        try {
            boolean result = this.quanziSearchService.synchroEsData();
            if (result) {
                ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("")
    public PageResult queryQuanziByKeyword(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                           @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                           @RequestParam String keyword) {

        try {
            return this.quanziSearchService.queryQuanziByKeyword(page,pageSize,keyword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
